package com.imgo.max.temporary.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.imgo.max.temporary.dao.SuperGirlCarDao;
import com.imgo.max.temporary.dao.SuperGirlCarManagerDao;
import com.imgo.max.temporary.dto.SuperGirlCarDto;
import com.imgo.max.user.dto.User;
import com.imgo.max.user.service.UserService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.imgo.max.temporary.dto.ActivityAreaDto;
import com.imgo.max.temporary.dto.StarDto;
import com.imgo.max.artist.dto.Artist;
import com.imgo.max.artist.service.PopularityService;
import com.imgo.max.common.dto.ResultCodeEnum;
import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.util.HttpClientUtil;
import com.imgo.max.core.tools.DateUtils;

@Service("superGirlCarService")
public class SuperGirlCarServiceImpl implements SuperGirlCarService {

	@Resource
	SuperGirlCarDao superGirlCarDao;
	
	@Resource PopularityService popularityService;
	
	@Resource SuperGirlCarManagerDao superGirlCarManagerDao;
	
	@Resource  UserService userService;

	@Override
	public ResultDO<String> saveApplySuperGirlCar(String uid, String zid,
			String phone, String userName) {
		ResultDO<String> resultdo = new ResultDO<String>();
		SuperGirlCarDto dto = new SuperGirlCarDto();
		dto.setUid(uid);
		dto.setAid(zid);
		dto.setPhoneNum(phone);
		dto.setUname(userName);
		dto.setStatus(1);
		try {
			// 活动地点id检测非空
			if (StringUtils.isBlank(dto.getAid())) {
				resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_PARAMS_ERROR
						.getResultCode());
				resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_PARAMS_ERROR
						.getMessage());
				resultdo.setSuccess(false);
				return resultdo;
			}

			// 报名艺人id检测非空
			if (StringUtils.isBlank(dto.getUid())) {
				resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_PARAMS_ERROR
						.getResultCode());
				resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_PARAMS_ERROR
						.getMessage());
				resultdo.setSuccess(false);
				return resultdo;
			}

			// 检测用户有没有报名过这个活动点
			resultdo = findRecordByAidAndUid(dto.getAid(), dto.getUid());
			if (!resultdo.isSuccess()) {
				return resultdo;
			}
			
			if (null!=resultdo.getModule() && !"0".equals(resultdo.getModule())) {
				// 失败
				resultdo.setSuccess(false);
				resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SINGED_ERROR
						.getResultCode());
				resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SINGED_ERROR
						.getMessage());
				return resultdo;
			}

			int flag = superGirlCarDao.saveApplySuperGirlCar(dto);
			if (flag != 1) {
				// 失败
				resultdo.setSuccess(false);
				resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_ERROR
						.getResultCode());
				resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_ERROR
						.getMessage());
				return resultdo;
			}
		} catch (Exception e) {
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_ERROR
					.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SIGN_ERROR
					.getMessage());
			resultdo.setErrorMessage(e.getMessage());
			resultdo.setSuccess(false);
			return resultdo;
		}
		resultdo.setModule("1");
		return resultdo;
	}

	@Override
	public ResultDO<String> findRecordByAidAndUid(String aid, String uid) {
		ResultDO<String> resultdo = new ResultDO<String>();
		try {
			SuperGirlCarDto dto = superGirlCarDao.findRecordByAidAndUid(aid, uid);
			if (null!=dto) {
				resultdo.setModule(dto.getStatus()+"");
			} else {
				resultdo.setModule("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_RECORD_QUERY_ERROR
					.getResultCode());
			resultdo.setErrorMessage(e.getMessage());
		}
		return resultdo;
	}

	@Override
	public ResultDO<String> findSingCount(String aid) {
		ResultDO<String> resultdo = new ResultDO<String>();
		int num = 0;
		try {
			if (StringUtils.isNotBlank(aid)) {
				num = superGirlCarDao.findCountSignSuperGirlCarStarByAid(aid);
			} else {
				num = superGirlCarDao.findCountSignSuperGirlCarStar();
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_SUM_COUNT_QUERY_ERROR.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_SUM_COUNT_QUERY_ERROR.getMessage());
			resultdo.setErrorMessage(e.getMessage());
		}
		resultdo.setModule(num + "");
		return resultdo;
	}

	@Override
	public ResultDO<List<SuperGirlCarDto>> queryJoinArtist(String zid) {
		ResultDO<List<SuperGirlCarDto>> resultdo = new ResultDO<List<SuperGirlCarDto>>();
		try {
			List<SuperGirlCarDto> dtoList =  superGirlCarManagerDao.queryJoinArtist(zid);
			List<String> uids = new ArrayList<String>();
			if(null!=dtoList && dtoList.size()>0){
				for (SuperGirlCarDto data : dtoList) {
					if(StringUtils.isNotBlank(data.getUid())){
						uids.add(data.getUid());
					}
				}
			}
			ResultDO<List<User>> usersResultDO = null;
			if(null!=uids && uids.size()>0){
				usersResultDO = userService.getArtist(uids);
			}
			
			if(null!=usersResultDO && null!=usersResultDO.getModule() && usersResultDO.getModule().size()>0){
				ArrayList<User> users = (ArrayList<User>) usersResultDO.getModule();
				Map<String, User> userMap = new HashMap<String, User>();
				for (User user : users) {
					userMap.put(user.getUid(), user);
				}
				
				for (SuperGirlCarDto data : dtoList) {
					if(null!=userMap.get(data.getUid())){
						if(StringUtils.isNotBlank(data.getUid())){
							User user= userMap.get(data.getUid());
							data.setUname(user.getNickName());
							data.setPhoneNum(user.getPhoneNum());
							data.setIdCard(user.getIdentity());
							data.setPhoto(user.getPhoto());
						}
					}
					
				}
			}
			resultdo.setModule(dtoList);
		} catch (Exception e) {
			e.printStackTrace();
			resultdo.setSuccess(false);
			resultdo.setResultCode(ResultCodeEnum.SUPER_GIRL_CAR_FIND_JOIN_ARTIST_ERROR.getResultCode());
			resultdo.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CAR_FIND_JOIN_ARTIST_ERROR.getMessage());
			resultdo.setErrorMessage(e.getMessage());
		}
		return resultdo;
	}
	
	
	@Override
	public ResultDO<List<StarDto>> findTop(String aid, String startTime, String endTime) {
		ResultDO<List<StarDto>> result = new ResultDO<List<StarDto>>();
		List<StarDto> ls = new ArrayList<StarDto>();
		try {
			ResultDO<List<Artist>>res = null;
			// 艺人 ids list
			List<String> uids = superGirlCarDao.findUidsByAid(aid);
			if(null!=uids && uids.size()>0){
				Date startDate = DateUtils.parseDate(startTime);
				Date endDate = DateUtils.parseDate(endTime);
				res =popularityService.queryArtistPopularity(startDate, endDate, new HashSet<String>(uids), 50);
			}
			StarDto dto = null;
			if(null!=res && null!=res.getModule() && res.getModule().size()>0){
				for (Artist artist:res.getModule()) {
					dto = new StarDto();
					dto.setRole(null!=artist.getRole()?artist.getRole()+"":"");
					dto.setHotValue(StringUtils.isNotBlank(artist.getHotValue())?artist.getHotValue():"");
					dto.setUid(artist.getUid());
					dto.setNickName(StringUtils.isNotBlank(artist.getNickName())?artist.getNickName():"");
					dto.setPhoto(StringUtils.isNotBlank(artist.getPhoto())?artist.getPhoto():"");
					ls.add(dto);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setSuccess(false);
			result.setResultCode(ResultCodeEnum.ARTIST_RANK_ERROR.getResultCode());
			result.setErrorMessage(ResultCodeEnum.ARTIST_RANK_ERROR.getMessage());
			result.setErrorMessage(e.getMessage());
		}
		result.setModule(ls);
		return result;
	}
	
	
	
	public List<ActivityAreaDto> getAllArea(){
		String str = HttpClientUtil.doGet("http://h5.max.mgtv.com/H5/20160314_mengniu_truck/js/data.js", null);
		int southStartIndex = str.indexOf("[");
		int southEndIndex = str.indexOf("]");
		int northStartIndex = str.lastIndexOf("[");
		int norEndIndex = str.lastIndexOf("]");
		System.out.println("----------------------------------------------");
		System.out.println("***"+str.substring(southStartIndex, southEndIndex+1));
		System.out.println("----------------------------------------------");
		System.out.println("***"+str.substring(northStartIndex, norEndIndex+1));
		return null;
	}

}
